/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DAO.Datacontact;
import DAO.NhanVienDAO;
import DAO.PhongBanDAO;
import DTO.clsNhanVien;
import DTO.clsPhongBan;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author XuanHung
 */
public class frmNhapNhanVienMoi extends javax.swing.JFrame {

    /**
     * Creates new form frmNhapNhanVienMoi
     */
    clsNhanVien nv;
    Datacontact dct;
    PhongBanDAO pbd;
    NhanVienDAO nvd;
    
    private void LoadData() throws Exception{
        List<clsPhongBan> lPhongBan  = new ArrayList<>();
        lPhongBan = pbd.getData();
        for(clsPhongBan cls : lPhongBan){
            jcbxPhong.addItem(cls.getM_iMaPhongBan());
        }
    }
    public frmNhapNhanVienMoi() throws Exception {
        initComponents();
        nv = new clsNhanVien();
        nvd = new NhanVienDAO();
        pbd = new  PhongBanDAO();
        boolean Connect = dct.Connect();
        LoadData();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jcbxPhong = new javax.swing.JComboBox();
        jbtnThem = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jtxtTenNhanVien = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jbtnThoat = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jtxtMaNhanVien = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jtxtNgaySinh = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jtxtChucVu = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jtxtLuongCanBan = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jcbxPhong.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select new leader" }));

        jbtnThem.setText("Thêm");
        jbtnThem.setActionCommand("");
        jbtnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnThemActionPerformed(evt);
            }
        });

        jLabel3.setText("Họ Tên Nhân Viên");

        jLabel4.setText("Phòng Ban");

        jtxtTenNhanVien.setText("TenNhanVien");

        jLabel1.setText("NHẬP NHÂN VIÊN MỚI");

        jbtnThoat.setText("Thoát");
        jbtnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnThoatActionPerformed(evt);
            }
        });

        jLabel2.setText("Mã Nhân Viên");

        jtxtMaNhanVien.setText("MaNhanVien");

        jLabel5.setText("Ngày Sinh");

        jtxtNgaySinh.setText("dd/mm/yyyy");

        jLabel6.setText("Chức Vụ");

        jtxtChucVu.setText("ChucVu");

        jLabel7.setText("Lương Căn Bản");

        jtxtLuongCanBan.setText("LuongCanBan");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jbtnThem)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jbtnThoat))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel4))
                                .addGap(26, 26, 26)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jcbxPhong, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jtxtTenNhanVien, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
                                    .addComponent(jtxtMaNhanVien)
                                    .addComponent(jtxtNgaySinh)
                                    .addComponent(jtxtChucVu)
                                    .addComponent(jtxtLuongCanBan)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(100, 100, 100)
                        .addComponent(jLabel1)))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtxtMaNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtxtTenNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jtxtNgaySinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jtxtChucVu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jtxtLuongCanBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jcbxPhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnThem)
                    .addComponent(jbtnThoat))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThemActionPerformed
        // TODO add your handling code here:
        try{
            int iMaNhanVien = Integer.parseInt(jtxtMaNhanVien.getText());
            String strHoTenNhanVien = jtxtTenNhanVien.getText();
            String strNgaySinh =  jtxtNgaySinh.getText();
            String strChucVu = jtxtChucVu.getText();
            double dbLuongCanBan = Double.parseDouble(jtxtLuongCanBan.getText());
            int iMaPhongBan = Integer.parseInt(jcbxPhong.getSelectedItem().toString());
            nv = new clsNhanVien(iMaNhanVien, strChucVu, iMaPhongBan, strChucVu, dbLuongCanBan, strNgaySinh);
            nvd.luuNhanVien(nv, false);
            try {
                LoadData();
            } catch (Exception ex) {
                Logger.getLogger(frmNhapNhanVienMoi.class.getName()).log(Level.SEVERE, null, ex);
            }
            dct.close();
        }
        catch(NumberFormatException | SQLException ex){
            try {
                throw new Exception(ex.toString());
            } catch (Exception ex1) {
                Logger.getLogger(frmNhapNhanVienMoi.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }           
        
    }//GEN-LAST:event_jbtnThemActionPerformed

    private void jbtnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThoatActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jbtnThoatActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmNhapNhanVienMoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmNhapNhanVienMoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmNhapNhanVienMoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmNhapNhanVienMoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmNhapNhanVienMoi().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JButton jbtnThem;
    private javax.swing.JButton jbtnThoat;
    private javax.swing.JComboBox jcbxPhong;
    private javax.swing.JTextField jtxtChucVu;
    private javax.swing.JTextField jtxtLuongCanBan;
    private javax.swing.JTextField jtxtMaNhanVien;
    private javax.swing.JTextField jtxtNgaySinh;
    private javax.swing.JTextField jtxtTenNhanVien;
    // End of variables declaration//GEN-END:variables
}
